package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.my.vote.R;
import com.my.vote.VoteApplication;
import com.my.vote.entity.EUser;
import com.my.vote.pst.user.IPstUser;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.IView;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 功能描述: 个人信息
 *
 * @author Xiao Jingxiang 肖景翔
 * @date 2018/4/11
 */
public class UserActivity extends AbstractActivity implements IPstUser.IVUser {
    @BindView(R.id.ivHead)
    ImageView ivHead;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvAccout)
    TextView tvAccout;

    public static void startUI(Context context) {
        Intent intent = new Intent(context, UserActivity.class);
        context.startActivity(intent);
    }

    public static void startUI(Context context, int toast) {
        Intent intent = new Intent(context, UserActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);

        final EUser user = AppCacheManager.newInstance().getCache(VoteApplication.USER, new EUser());
        tvName.setText(user.getUserName());
        tvAccout.setText(user.getAccount());
        if (TextUtils.isEmpty(user.getHeadUrl())) {
            ivHead.setImageResource(R.mipmap.ic_launcher_round);
        } else {
            Picasso.with(this).load(user.getHeadUrl()).into(ivHead);
        }

    }

    IPstUser pstUser;

    @OnClick(R.id.ivHead)
    public void onViewClicked() {
        MultiImageSelector.create().single().showCamera(true).start(this, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            if (pstUser == null || path == null || path.isEmpty()) {
                return;
            }
            pstUser.updateFile(path.get(0));
            Picasso.with(this).load(new File(path.get(0))).into(ivHead);
        }
    }



    @Override
    public IPresenter getPresenter() {
        if (pstUser == null) {
            pstUser = LoaderService.getService(IPstUser.class);
        }
        return pstUser;
    }
}
